What is @rails/actioncable?
@rails/actioncable is a JavaScript client for Action Cable, which is a framework for handling WebSockets in Ruby on Rails applications. It allows you to create real-time features in your Rails applications by providing a way to communicate between the server and the client using WebSockets.
What are @rails/actioncable's main functionalities?
Establishing a WebSocket Connection
This code establishes a WebSocket connection to the specified URL. The `createConsumer` method is used to create a new consumer instance that will manage the WebSocket connection.
const cable = ActionCable.createConsumer('ws://cable.example.com');
Subscribing to a Channel
This code subscribes to a channel named 'ChatChannel'. The `received` function is called whenever data is broadcasted to the channel. In this example, it simply logs the received data to the console.
const subscription = cable.subscriptions.create('ChatChannel', {
received(data) {
console.log('Received:', data);
}
});
Sending Data to the Server
This code sends data to the server through the WebSocket connection. The `send` method is used to transmit a message to the server, which can then be processed and broadcasted to other clients.
subscription.send({ message: 'Hello, World!' });
Unsubscribing from a Channel
This code unsubscribes from the 'ChatChannel'. The `unsubscribe` method is used to terminate the subscription and stop receiving data from the channel.
subscription.unsubscribe();
Other packages similar to @rails/actioncable
socket.io-client
Socket.IO is a popular library for real-time web applications. It enables real-time, bidirectional communication between web clients and servers. Compared to @rails/actioncable, Socket.IO offers more features and flexibility, including support for rooms, namespaces, and various transport protocols.
faye-websocket
Faye WebSocket is a simple WebSocket client and server implementation. It is more lightweight compared to @rails/actioncable and is suitable for applications that need basic WebSocket functionality without the additional features provided by Action Cable.
phoenix-channels
Phoenix Channels is a part of the Phoenix framework for Elixir, providing real-time communication capabilities. It is similar to @rails/actioncable in that it integrates tightly with its respective web framework, offering a seamless way to add real-time features to Phoenix applications.
Action Cable – Integrated WebSockets for Rails
Action Cable seamlessly integrates WebSockets with the rest of your Rails application.
It allows for real-time features to be written in Ruby in the same style
and form as the rest of your Rails application, while still being performant
and scalable. It's a full-stack offering that provides both a client-side
JavaScript framework and a server-side Ruby framework. You have access to your full
domain model written with Active Record or your ORM of choice.
You can read more about Action Cable in the Action Cable Overview guide.
Support
API documentation is at:
Bug reports for the Ruby on Rails project can be filed here:
Feature requests should be discussed on the rails-core mailing list here: